package com.moshang.blog.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.entity.ThirdLoginUser;
import com.moshang.blog.mapper.ThirdLoginUserMapper;
import com.moshang.blog.service.ThirdLoginUserService;
import com.moshang.blog.core.utils.HttpUtil;
import com.moshang.blog.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @description: ${description}
 * @author: xieweiwei
 * @create: 2018-10-18 12:33
 **/
@Service("thirdLoginUserService")
public class ThirdLoginUserServiceImpl extends ServiceImpl<ThirdLoginUserMapper,ThirdLoginUser> implements ThirdLoginUserService {

    private static final String ACCESS_TOKEN_URL="https://github.com/login/oauth/access_token";
    private static final String GET_USER_URL="https://api.github.com/user?access_token=";

    @Resource
    private ThirdLoginUserMapper thirdLoginUserMapper;
    @Override
    public ThirdLoginUser findBycondition(String uid, String loginType, Integer status) {
        QueryWrapper<ThirdLoginUser> param=new QueryWrapper();
        param.eq("open_id",uid);
        param.eq("login_type",loginType);
        param.eq("status",status);
        ThirdLoginUser  thirdLoginUser= thirdLoginUserMapper.selectOne(param);
        if (thirdLoginUser!=null){
            return thirdLoginUser;
        }
        return null;
    }
    /**
     * 获取github的accessToken
     * @param code
     * @return
     */
    @Override
    public String getAccessToken(String code, String githubClientId, String githubClientSecret, String githubRedictUrl) {
        String accessToken="";
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put("client_id", githubClientId);
        formMap.put("client_secret", githubClientSecret);
        formMap.put("redirect_uri", githubRedictUrl);
        formMap.put("code", code);
        String result = HttpUtil.post(ACCESS_TOKEN_URL, formMap);
        // access_token=3fd14272cf9e074dc42301061fd01574979a7b56&scope=&token_type=bearer
        Map<String, String> map = StringUtils.getParam(result);
       accessToken = map.get("access_token");
        return  accessToken;
    }

    /**
     * github获取用户信息，这一步不能用post请求，否则得到的是{"message":"Requires authentication","documentation_url":"https://developer.
     * @param accessToken
     * @param o
     * @return
     */
    @Override
    public ThirdLoginUser getUserInfo(String accessToken, Object o) {
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put("access_token", accessToken);
        String result = HttpUtil.get(GET_USER_URL+accessToken);
        JSONObject data = com.alibaba.fastjson.JSONObject.parseObject(result);
        ThirdLoginUser thirdLoginUser=new ThirdLoginUser();
        thirdLoginUser.setOpenId(data.getString("id"));
        thirdLoginUser.setAvatar(data.getString("avatar_url"));
        thirdLoginUser.setNickName(data.getString("login"));
        return thirdLoginUser;
    }

    @Override
    public void updateDate(ThirdLoginUser param) {
        thirdLoginUserMapper.updateById(param);
    }

    @Override
    public List<ThirdLoginUser> findByStatus(Integer status) {
       QueryWrapper<ThirdLoginUser> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("status",status);
        entityWrapper.orderByDesc("last_login_time");
        List<ThirdLoginUser>  list= thirdLoginUserMapper.selectList(entityWrapper);
        return list;
    }
}
